//functions.h
/********************************************************************************
*																				*
*	Author:			Jared Szechy												*
*	Assignment:		Assignment 4												*
*	Class:			CPS-151														*
*	Instructor:		V. Starkley													*
*	Due Date:		October 3													*
*																				*
*	This file defines and impelement the program functions.						*
*																				*
********************************************************************************/
#include <string>
using namespace std;

#include "stack.h"


template <class ItemType>
void fillDispenser(Stack<ItemType>& dispenser);
//pre: none;
//post: fills the dispenser with MAX_ITEMS candies

template <class ItemType>
int findItems(Stack<ItemType> dispenser, int numCandies, string select);
//pre: none
//post: returns the number of candies found in the stack

template <class ItemType>
void getCandies(Stack<ItemType> origDisp, Stack<ItemType>& newDisp, int found, string select);
//pre: none
//post: pops the requested candy flavor and places the others back in the stack

template <class ItemType>
void printStacks(Stack<ItemType> origDisp, Stack<ItemType> newDisp);
//pre: none
//post: prints the two stacks to the screen in 2 columns

template <class ItemType>
void fillDispenser(Stack<ItemType>& dispenser)
{
	int num;

	for (int i = 0; i < MAX_ITEMS; i++)
	{
		num = 1 + rand() % 5;
		
		switch (num)
		{
		case 1:
            dispenser.Push("lime");
			break;
		case 2:
			dispenser.Push("cherry");
			break;
		case 3:
			dispenser.Push("orange");
			break;
		case 4:
			dispenser.Push("grape");
			break;
		case 5:
			dispenser.Push("lemon");
			break;
		}

	}
}

template<class ItemType>
int findItems(Stack<ItemType> dispenser, int numCandies, string select)
{
	int found = 0;
	string popped;

	while (found < numCandies && !dispenser.IsEmpty())
	{
		dispenser.Pop(popped);
		if (popped == select)
			found++;
	}

	return found;
}

template <class ItemType>
void getCandies(Stack<ItemType> origDisp, Stack<ItemType>& newDisp, int found, string select)
{
	string popped;
	int count = 0;
	Stack<ItemType> tempDisp;

	for (int i = 0; i < MAX_ITEMS; i++)
	{
		origDisp.Pop(popped);
		if (popped != select)
			tempDisp.Push(popped);
		else if (count >= found)
			tempDisp.Push(popped);
		else
			count++;
	}

	while (!tempDisp.IsEmpty())
	{
		tempDisp.Pop(popped);
		newDisp.Push(popped);
	}
}

template <class ItemType>
void printStacks(Stack<ItemType> origDisp, Stack<ItemType> newDisp)
{
	string popped;
	cout << endl << "Original Dispenser" << "\t" << "Current Dispenser" << endl;
	for (int i = 0; i < MAX_ITEMS; i++)
	{
		origDisp.Pop(popped);
		cout << popped << "\t\t\t";

		if (!newDisp.IsEmpty())
		{
			newDisp.Pop(popped);
			cout << popped << endl;
		}
		else
			cout << endl;
	}
	cout << endl;
}